package com.mustafaiev.tair.autobase.db;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.h2.jdbcx.JdbcConnectionPool;

/**
 * 
 * The class provides creating connection pool, start of DB connection , stop of
 * DB connection, and close connection, statement and result set
 * 
 * @author Tair Mustafaiev
 * 
 */
public class DBConnector {

    private static JdbcConnectionPool connectionPool;

    public static final String DB_SETTINGS = "/configurations/db.properties";

    private static final Logger LOG = Logger.getLogger(DBConnector.class.getName());

    /**
     * Default DBconnector constructor
     */
    private DBConnector() {
    }

    /**
     * 
     * @return new BDConnector instance
     */
    public static DBConnector getInstance() {
        return new DBConnector();
    }

    /**
     * start connection
     * 
     * @throws SQLException
     */
    public void start() throws SQLException {
        JdbcConnectionPool cp = getConnectionPool();
        cp.setMaxConnections(20);
    }

    public Connection getConnection() throws SQLException {
        Connection connection = getConnectionPool().getConnection();
        return connection;
    }

    public void stop() {
        try {
            getConnectionPool().dispose();
        } catch (SQLException e) {
            LOG.error(e);
        }
    }

    /**
     * close connection
     * 
     * @param connection
     */
    public void close(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                LOG.error(e);
            }
        }
    }

    /**
     * close statement
     * 
     * @param statement
     */
    public void close(PreparedStatement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                LOG.error(e);
            }
        }
    }

    /**
     * close ResultSet
     * 
     * @param resultSet
     */
    public void close(ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                LOG.error(e);
            }
        }
    }
    
    /**
     * close Statement
     * 
     * @param resultSet
     */
    public void close(Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                LOG.error(e);
            }
        }
    }

    /**
     * @return Connection
     * @throws IOException
     */
    private static JdbcConnectionPool createPool() throws SQLException {
        Properties dbProps = new Properties();

        try {
            dbProps.load(DBConnector.class.getResourceAsStream(DB_SETTINGS));
            Class.forName("org.h2.Driver");
        } catch (IOException e) {
            LOG.error("DB settings file not found!");
            throw new SQLException();
        } catch (ClassNotFoundException e) {
            LOG.error("DB driver class not found!");
            throw new SQLException();
        }

        return JdbcConnectionPool.create(dbProps.getProperty("url"), "sa", "");
    }

    synchronized private static JdbcConnectionPool getConnectionPool() throws SQLException {
        if (connectionPool != null) {
            return connectionPool;
        } else {
            return connectionPool = createPool();
        }
    }
}
